package com.beesphere.excel.writer.tests;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.beesphere.excel.reader.CellType;
import com.beesphere.excel.writer.ExcelWriterException;
import com.beesphere.excel.writer.LineCell;
import com.beesphere.excel.writer.impls.xml.XmlToExcelConverter;
import com.beesphere.excel.writer.style.CellStyleRegistry;
import com.beesphere.excel.writer.style.CellStyleSpecReaderException;
import com.beesphere.excel.writer.style.JsonCellStyleRegistry;
import com.qlogic.commons.utils.dates.DateUtils;

public class StyleTest {
	
	public static void main (String[] args) throws ExcelWriterException, IOException, CellStyleSpecReaderException {
		// Styles
		CellStyleRegistry sr = new JsonCellStyleRegistry ();
		String styles = "{S-001: {\"fillForegroundColor\": 'yellow', \"fillPattern\" : 'solid_foreground'" +
				", format: 'mm/dd/yyyy hh:mm:ss', border : {bottom: {type: 'small_dashed'}}" +
				", \"alignment\": 'left', indention: 40, \"font\" : {name: \"Verdana\", height: 50, heightInPoints: 10, italic: true, underline: 'double', boldweight: 'bold'} }}";
		sr.install (styles);
		
		OutputStream output = new FileOutputStream ("files/out.xls");
		XmlToExcelConverter converter = new XmlToExcelConverter (output) {
			private static final long serialVersionUID = -5276084952600590668L;

			@Override
			protected void visitCell (LineCell cell, int index) throws ExcelWriterException {
				cell.addInfo (LineCell.STYLE, "S-001");
				if (index == 3) {
					cell.addInfo (LineCell.TYPE, CellType.DATE);
					cell.addInfo (LineCell.VALUE, DateUtils.toDate((String)cell.getInfo(LineCell.VALUE), "dd/MM/yyyy HH:mm:ss"));
				}
			}
		};
		converter.setStyleRegistry (sr);
		converter.setInstallCustomFormats(true);
		//converter.setOoxml(true);
		Map<String, String> tags = new HashMap<String, String> ();
		tags.put("wb", "root");
		tags.put("sh", "data");
		tags.put("rs", "products");
		tags.put("r", "product");
		converter.setTags(tags);
		converter.setCharset (Charset.defaultCharset());
		converter.write (new FileInputStream ("files/in.xml"));
		output.close ();
	}
	
}
